package com.heima.comment.service.impl;

import com.alibaba.fastjson.JSON;
import com.heima.feign.wemedia.IWemediaClient;
import com.heima.model.wemedia.pojos.WmBaiduToken;;
import com.heima.utils.baidu.HttpUtil;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.net.URLEncoder;
import java.util.Map;

/**
 * @author 陈辉
 * @data 2023 19:39
 */
@Service
@Slf4j
public class GreenScanServiceImpl {

    @Autowired
    private IWemediaClient iWemediaClient;
    /**
     * 审核文本
     *
     * @param text
     * @return
     */
    public Map scanText(String text) {
        // 请求url
        String url = "https://aip.baidubce.com/rest/2.0/solution/v1/text_censor/v2/user_defined";
        try {
            String param = "text=" + URLEncoder.encode(text, "utf-8");

            // 注意这里仅为了简化编码每一次请求都去获取access_token，线上环境access_token有过期时间， 客户端可自行缓存，过期后重新获取。
            WmBaiduToken wmBaiduToken = iWemediaClient.getToken();
            String accessToken = wmBaiduToken.getToken();

            String result = HttpUtil.post(url, accessToken, param);
            Map map = JSON.parseObject(result, Map.class);
            return map;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
